package com.heima.controller;

import com.heima.annotation.OperationFill;
import com.heima.dto.ClueDto;
import com.heima.dto.FalseClueReasonDto;
import com.heima.dto.ClueSelectDto;
import com.heima.dto.pagedto.CluePageDto;
import com.heima.pojo.Activities;
import com.heima.result.PageResult;
import com.heima.result.Result;
import com.heima.service.ClueService;
import com.heima.vo.ClueVo;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/clues")
@Api(tags = "线索模块")
public class ClueController {
    private final ClueService clueService;

    /**
     * 添加线索
     * @param clueDto
     * @return
     */
    @PostMapping("/clue")
    @OperationFill
    public Result add(@RequestBody ClueDto clueDto) {
        clueService.add(clueDto);
        return Result.success();
    }

    /**
     * 分页查询
     * @param cluePageDto
     * @return
     */
    @GetMapping("/clue/list")
    @OperationFill
    public PageResult pageQuery(CluePageDto cluePageDto,
                                @RequestParam(value = "params[beginCreateTime]", required = false) String begin,
                                @RequestParam(value = "params[endCreateTime]", required = false) String end) {
        cluePageDto.setBeginCreateTime(begin);
        cluePageDto.setEndCreateTime(end);
        PageResult pageResult = clueService.pageQuery(cluePageDto);
        return pageResult;
    }


    /**
     * 通过 excel 文件批量上传线索
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping("/clue/importData")
    @OperationFill
    public Result addBatch(MultipartFile file) throws IOException {
        clueService.addBatch(file);
        return Result.success();
    }


    /**
     * 根据 活动 id 返回具体的活动信息
     * @param channelId
     * @return
     */
    @GetMapping("/activity/listselect/{channelId}")
    @OperationFill
    public Result queryActivityByChannelId(@PathVariable Integer channelId) {
        List<Activities> activitiesList = clueService.queryActivityListByChannelId(channelId);
        return Result.success(activitiesList);
    }

    /**
     * 根据 id 查询对应的线索信息
     * @return
     */
    @GetMapping("/clue/{id}")
    public Result queryById(@PathVariable Integer id) {
        ClueVo clueVo = clueService.queryById(id);
        return Result.success(clueVo);
    }

    @GetMapping("/record/list")
    public Result isQueryByIdSuccessful(Integer clueId) {
        ClueVo clueVo = clueService.queryById(clueId);
        if (clueVo != null) {
            return Result.success(clueVo);
        }
        return null;
    }

    /**
     * 通过 id 将线索设置为 伪线索
     * @param id
     * @return
     */
    @PutMapping("/clue/false/{id}")
    public Result falseClueSetting(@PathVariable Integer id, @RequestBody FalseClueReasonDto reason) {
        clueService.falseClueSetting(id, reason);
        return Result.success();
    }

    /**
     * 跟进线索, 并使得状态更改为跟进中
     * @return
     */
    @PostMapping("/record")
    public Result record(@RequestBody ClueDto clueDto) {
        clueService.record(clueDto);
        return Result.success();
    }

    /**
     * 线索池分页查询
     * @param cluePageDto
     * @param begin
     * @param end
     * @return
     */
    @GetMapping("/clue/pool")
    public PageResult poolPageQuery(CluePageDto cluePageDto,
                                    @RequestParam(value = "params[beginCreateTime]", required = false) String begin,
                                    @RequestParam(value = "params[endCreateTime]", required = false) String end) {
        PageResult pageResult = clueService.poolPageQuery(cluePageDto, begin, end);
        return pageResult;
    }

    /**
     * 批量捞取
     * @param clueSelectDto
     * @return
     */
    @PutMapping("/clue/gain")
    public Result batchGain(@RequestBody ClueSelectDto clueSelectDto) {
        clueService.batchGain(clueSelectDto.getIds());
        return Result.success();
    }

    /**
     * 线索转变为商机
     * @param id
     * @return
     */
    @PutMapping("/clue/changeBusiness/{id}")
    public Result changeBusiness(@PathVariable Integer id) {
        clueService.changeBusiness(id);
        return Result.success();
    }

    @PutMapping("/clue/assignment")
    public Result assignment(@RequestBody ClueSelectDto clueSelectDto) {
        clueService.assignment(clueSelectDto);
        return Result.success();
    }


}
